package cn.edu.zut.cs;

import java.util.List;

import cn.edu.zut.cs.Model.Student;
import cn.edu.zut.cs.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/student")
public class StudentCon {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String viewAllStudents(Model model) {
        List<Student> students = studentService.queryAll();
        model.addAttribute("students", students);
        return "studentList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addNewStudent(Model model) {
        Student student = new Student();
        model.addAttribute("student", student);
        return "studentForm";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editStudent(Model model, int rollNo) {
        Student student = studentService.getStudent(rollNo);
        model.addAttribute("student", student);
        return "studentForm";
    }


    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String deleteStudent(Student student) {
        studentService.delete(student);
        return "redirect:/student/list";
    }
}